/**
 * 页面的初始数据
 * auth：晚空
 * web:www.mmifx.com
 */
const app = getApp();
// 初始化广告
// let interstitialAd = null
// 引入腾讯语音接口
var plugin = requirePlugin("WechatSI")
let Record = plugin.getRecordRecognitionManager()

// 检测程序更新
const updateManager = wx.getUpdateManager()
updateManager.onUpdateReady(function () {
  wx.showModal({
    title: '更新提示',
    content: '检测到新版本，点击确定更新重启应用获得全新体验！',
    success: function (res) {
      updateManager.applyUpdate()
    }
  })
})
Page({

  data: {
    status: 1, //全程状态
    showRes: false, //显示结果集
    HiddenAd1: false, //隐藏广告1
    HiddenAd2: false, //隐藏广告2
    searchdcss: '', //动画效果
    resfade: "fade_out", //动画效果
    question: "", //绑定问题
    reslen: 0, //结果数组长度
    toptips: "", //顶部提示内容
    topicShow: true, //显示顶部提示信息
    toptipstype: "error", //顶部提示类型
    token: "6a17eb58468a893ccc8aacf23c7f2f10f90872f2", //搜题token
    modeltips: {}, //远程弹窗
    showad: false, //显示广告
    res: null,
    ad1unit: "", //广告1
    ad2unit: "", //广告2
    // 遮罩问题详情
    CoverisShow: false,
    nowquestion: "问题测试",
    nowanswer: "答案测试",
    nowmore: "",
    imgsrc: "",
    recordConfig: {
      show: false,
      istouch: false,
      title: "按住以进行语音识别",
      res: ""
    },
  },
  // 获取开始数据
  GetStartInfo: function () {
    let that = this;
    // 本地存储
    // const result = {
    //   status: 1,
    //   msg: {
    //     status: 1,
    //     version: 1,
    //     title: "温馨提示",
    //     content: "小程序已恢复正常使用，全新改版的视觉效果希望您喜欢！"
    //   },
    //   token: "c36f92797a2316d715520c57c7e6c190b6b91fc6",
    //   showad: true,
    //   ad1unit: "adunit-7d76004942b2f5bc",
    //   ad2unit: "adunit-9594f68cc580d15f"
    // }
    // that.setData({
    //   status: result.data.status,
    //   token: result.data.token,
    //   modeltips: result.data.msg,
    //   ad1unit: result.data.ad1unit,
    //   ad2unit: result.data.ad2unit,
    //   showad: result.data.showad,
    // })
    // 本次存储结束
    // 使用服务器的话：
    wx.request({
      url: '',
      data: {},
      header: {
        'content-type': 'application/json'
      },
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: (result) => {
        that.setData({
          status: result.data.status,
          token: result.data.token,
          modeltips: result.data.msg,
          ad1unit: result.data.ad1unit,
          ad2unit: result.data.ad2unit,
          showad: result.data.showad,
        })
        that.CheckModelTips()
      },
      fail: () => {
        // wx.showModal({
        //   title: "提示",
        //   content: "网络连接失败,您可能无法使用完整功能,请检查网络后重试！",
        // })
      },
      complete: () => {}
    });
    // 服务器存储结束
  },
  // 关闭问题遮罩
  closecover: function () {
    let that = this;
    that.setData({
      CoverisShow: false
    })
  },
  // 复制答案
  copyanswer: function () {
    let that = this;
    wx.setClipboardData({
      data: that.data.nowanswer,
      success: (res) => {},
      fail: () => {
        that.setData({
          toptips: "复制到剪切板失败！请重试",
          topicShow: true,
          toptipstype: "error"
        })
      }
    })
  },
  // 检测弹出框
  CheckModelTips: function () {
    let that = this;
    wx.getStorage({
      key: "tip" + that.data.modeltips.version.toString(),
      success: function (res) {
        if (res.data != that.data.modeltips.content) {
          wx.showModal({
            title: "宝",
            content: "欢迎你使用最全网课!"
          })
        }
      },
      fail: (res) => {
        // 设置缓存数据
        wx.setStorage({
          key: "tip" + that.data.modeltips.version.toString(),
          data: that.data.modeltips.content,
          success: (res) => {
            console.log("存储本地信息成功", res)
          },
          fail: (e) => {
            console.log("存储本地信息失败", e)
          },
          complete: () => {
            if (that.data.modeltips.status) {
              wx.showModal({
                title: that.data.modeltips.title,
                content: that.data.modeltips.content
              })
            }
          }
        })

      }
    })
  },
  // 前往帮助页面
  gohelp: function () {
    wx.navigateTo({
      url: '/pages/index/help/help',
    })
  },
  // 设置logo向上偏移
  SetLogoPosition: function () {
    let that = this;
    if (that.data.searchdcss == "main_res") {
      that.setData({
        searchdcss: ""
      })
    } else {
      that.setData({
        searchdcss: "main_res"
      })
    }
  },
  // 设置是否显示结果集
  SetShowRes: function () {
    this.setData({
      showRes: !this.data.showRes
    })
  },
  // 设置fade效果
  SetFade: function () {
    let that = this;
    if (that.data.resfade == "fade_in") {
      that.setData({
        resfade: "fade_out"
      })
    } else {
      that.setData({
        resfade: "fade_in"
      })
    }
  },
  // 监控输入框内容
  qinput: function (e) {
    this.setData({
      question: e.detail.value
    })
  },
  // 发起搜素
  search() {
    let that = this;
    if (that.CheckQuestionEmpty()) {
      that.setData({
        toptips: "问题不能为空！",
        topicShow: true,
        toptipstype: "error"
      })
    } else {
      // if (interstitialAd) {
      //   interstitialAd.show()
      // }
      that.GetAnswer();
    }
  },
  // 检测问题是否为空
  CheckQuestionEmpty: function () {
    if (this.data.question == "") {
      return true;
    } else {
      return false;
    }
  },

  // 获取数据
  GetAnswer: function () {
    let that = this;
    wx.showLoading({
      title: '搜索中...',
      mask: true
    })
    wx.request({
      url: 'https://sou.mmifx.com/api.php',
      data: {
        limit: 10,
        token: that.data.token,
        question: that.data.question
      },
      header: {
        'content-type': 'application/json'
      },
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: (result) => {
        if (result.data.code != 200) {
          that.setData({
            toptips: result.data.msg.title,
            topicShow: true,
            toptipstype: "error"
          })
        } else {
          that.setData({
            toptips: result.data.msg.title,
            topicShow: true,
            toptipstype: "success",
            res: result.data.data,
            reslen: result.data.data.length
          })
          if (!that.data.showRes) {
            that.SetLogoPosition();
            that.SetShowRes();
            that.SetFade();
          }
        }
      },
      fail: () => {
        wx.showModal({
          title: "提示",
          content: "网络链接失败！请检查网络后重试",
        })
      },
      complete: () => {
        wx.hideLoading();
      }
    });
  },
  inputfocus: function () {
    let that = this;
    if (that.data.showRes) {
      that.SetLogoPosition();
      that.SetShowRes();
      that.SetFade();
    }
  },
  // 清除问题
  clearques: function () {
    let that = this;
    that.setData({
      question: "",
      res: null,
    })
    if (that.data.showRes) {
      that.SetLogoPosition();
      that.SetShowRes();
      that.SetFade();
    }
  },
  // 获取问题详细信息
  GetDetail: function (e) {
    let that = this;
    let info = e.target.dataset;
    that.setData({
      CoverisShow: true,
      nowquestion: info.question,
      nowanswer: info.answer,
      nowmore: info.more
    })
  },
  // 录音图标获取到焦点事件
  handleRecordStart: function (e) {
    this.setData({
      'recordConfig.istouch': true,
      'recordConfig.title': "正在进行录音，松开即可结束"

    })
    Record.start();
    // 
    console.log("开始录音");
  },
  // 失去焦点
  handleRecordEnd: function (e) {
    this.setData({
      'recordConfig.istouch': false,
      'recordConfig.title': "按住以进行语音识别"
    })
    Record.stop();
    // 
    console.log("结束录音");
  },
  // 点击遮罩关闭
  handleRecordPress: function (e) {
    this.setData({
      recordPress: true
    })
  },
  // 语音识别
  getvoice: function () {
    let that = this;
    that.setData({
      'recordConfig.show': true
    })
    Record.onStop = function (res) {
      console.log(res);
      // 判断是否存在有效内容
      if (res.result == "") {
        wx.showModal({
          title: "提示",
          content: "未提取到文字信息，请重试:(",
          showCancel: false
        })
      } else {
        wx.showModal({
          title: "识别完成",
          content: res.result,
          cancelText: "重录",
          confirmText: "搜索",
          success(mores) {
            if (mores.confirm) {
              // 发起搜索
              that.setData({
                question: res.result,
                'recordConfig.show': false
              })
              that.search()
            }
          }
        })
      }
    }
    Record.onStart = function (res) {
      console.log("成功开始录音识别", res)
    }
  },

  // 拍照识别。。
  getphoto: function () {
    let that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        wx.getFileSystemManager().readFile({
          filePath: res.tempFilePaths[0],
          encoding: 'base64',
          success: res => {
            that.getOrcRes(res.data)
          },
          fail() {
            that.setData({
              toptips: "图片转码失败。",
              topicShow: true,
              toptipstype: "error"
            })
          }
        })
      },
      fail() {
        that.setData({
          toptips: "调用相册失败，请重试！",
          topicShow: true,
          toptipstype: "error"
        })
      }
    })
  },
  // 获取图片上传后解析结果
  getOrcRes(base64data) {
    let that = this;
    wx.showLoading({
      title: '解析图片中...',
      mask: true
    })
    wx.request({
      url: "https://wx.mmifx.com/api/baidu_ocr.php",
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        basecode: base64data
      },
      success(res) {
        res = res.data
        if (res.code !== 200) {
          that.setData({
            toptips: res.msg,
            topicShow: true,
            toptipstype: "error"
          })
        } else {
          that.setData({
            toptips: "提取文字成功！",
            topicShow: true,
            toptipstype: "success"
          })
          if(res.meta.length>6){
            res.meta=res.meta.slice(0,6)
          }
          var wordslist = []
          res.meta.forEach(item => {
            wordslist.push(item.words)
          })
          wx.showActionSheet({
            itemList: wordslist,
            success(res) {
              console.log(res.tapIndex)
              that.setData({
                question: wordslist[res.tapIndex]
              })
              that.search()
            },
            fail(res) {
              console.log(res.errMsg)
            }
          })
        }
      },
      complete() {
        wx.hideLoading()
      }
    })
  },
  // 弹出层其余地区点击
  CoverHidden:function(){
    let that=this;
    that.setData({
      CoverisShow:false
    })
  },
  valueSelect:function(){
    console.log("防止冒泡");
  },
  // 监控剪切板
  listencut: function () {
    let that = this;
    wx.getStorage({
      key: "cutlisten",
      success: (res) => {
        if (res.data == "开启") {
          wx.getClipboardData({
            success(res) {
              if (res.data !== '') {
                wx.showModal({
                  title: '检测到剪切板内容，是否搜索？',
                  content: res.data,
                  cancelText: '取消',
                  confirmText: '搜索',
                  success(resyes) {
                    if (resyes.confirm) {
                      that.setData({
                        question: res.data
                      })
                      that.search();
                    } else if (resyes.cancel) {
                      console.log('用户点击了取消按钮');
                    }
                  }
                })
              }
            }
          })
        }
      },
      fail: (res) => {

      }
    })
  },
  // 广告部分函数
  // 广告位1加载成功
  ad1load() {
    let that = this;
    that.setData({
      HiddenAd1: false
    })
  },
  // 广告位2加载成功
  ad2load() {
    let that = this;
    that.setData({
      HiddenAd2: false
    })
  },
  // 广告位1加载失败
  ad1error(err) {
    let that = this;
    that.setData({
      HiddenAd1: true
    })
  },
  // 广告2加载失败
  ad2error(err) {
    let that = this;
    that.setData({
      HiddenAd2: true
    })
  },
  // 广告1被关闭
  ad1close() {
    let that = this;
    that.setData({
      HiddenAd1: true
    })
  },
  // 广告1被关闭
  ad2close() {
    let that = this;
    that.setData({
      HiddenAd2: true
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.GetStartInfo();
    // if (wx.createInterstitialAd) {
    //   interstitialAd = wx.createInterstitialAd({
    //     adUnitId: 'adunit-a74c8e0f79917102'
    //   })
    //   interstitialAd.onLoad(() => {})
    //   interstitialAd.onError((err) => {})
    //   interstitialAd.onClose(() => {})
    // }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.listencut()
    this.setData({
      showalert: true
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
// 录音转文字